/**
 * @file encrypt.js 加密
 * @example
 *  aesEncrypt(data)
 */

const crypto = require('crypto')
const key = 'f0f8ab4932884d54a5504fece6ef61ce'

/**
 * aes-256-ecb 加密
 * @param {string} data 待加密数据
 * @param {?string} iv Initial Vector
 * @return {string} 加密后数据
 */
export function aesEncrypt(data, iv = '') {
  iv = iv || ''
  var clearEncoding = 'utf8'
  var cipherEncoding = 'base64'
  var cipherChunks = []
  var cipher = crypto.createCipheriv('aes-256-ecb', key, iv)
  cipher.setAutoPadding(true)
  cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding))
  cipherChunks.push(cipher.final(cipherEncoding))
  return cipherChunks.join('')
}
